let fs = require("fs");
let path = require("path");
let result = {};

// 获取的当前项目的根目录
result.rootPath = path.resolve(__dirname, "..");
// 获取每一个文件夹下的文件
result.getFiles = (dir) => {
    let files = fs.readdirSync(dir);
    let res = [];
    for (let i = 0; i < files.length; i++) {
        let file = files[i];
        let filePath = path.resolve(dir, file);
        let stat = fs.statSync(filePath);
        if (stat.isDirectory()) {
            res = res.concat(result.getFiles(filePath));
        } else {
            res.push(filePath);
        }
    }
    return res;
}

// 获取所有api_开头的文件
result.getApiFiles = ()=>{
    // 获取所有文件
    let allFiles = result.getFiles(result.rootPath);
    // 获取所有api_开头的文件
    return allFiles.filter((file) => {
        let filename = path.basename(file);
        return filename.startsWith("api_")&&filename.endsWith(".js");
    }).map((file) => {
        return path.relative(result.rootPath, file);
    });
}

module.exports = result;